<?php
define('EZSITETPL_SOAP', 1);
$client_ip = $_SERVER['REMOTE_ADDR'];
//echo '$client_ip='.$client_ip;

function checkDns2($dnsRecord,$ips){
	
	global $client_ip;
	
	$domain_ip = gethostbyname($dnsRecord['domain']);
	//$flag = 0;
	//echo '$client_ip222='.$client_ip;
	//echo '$domain_ip='.$domain_ip;
	//print_r($dnsRecord);
	if(in_array($client_ip,$ips)){
		return false;
	}
	return true;
}

function checkDns($dnsRecord){
	
	global $client_ip;
	
	$domain_ip = gethostbyname($dnsRecord['domain']);
	//$flag = 0;
	//echo '$client_ip222='.$client_ip;
	//echo '$domain_ip='.$domain_ip;
	//print_r($dnsRecord);
	//if(in_array($domain_ip,$ips) && in_array($client_ip,$ips)){
	//	return false;
	//}
	/*
	if(in_array('447f32d25b7d55524675c64d649ae5d9',$dnsRecord)){
		return false;
	}
	*/
	if(in_array($domain_ip,$dnsRecord) && in_array($client_ip,$dnsRecord)){
		return false;
	}else{
		if($domain_ip==$client_ip){
			return false;
		}
		return true;
	}
	/*
	foreach($dnsRecord as $val){
		$__arr = explode(".",$val);
		if($__arr[0]=='192'||$__arr[0]=='10'||$__arr[0]=='172'||$__arr[0]=='447f32d25b7d55524675c64d649ae5d9'){
			$flag = 1;
		}
	}
	if($flag == 1){
		if($domain_ip==$client_ip || in_array($domain_ip,$dnsRecord)){//echo "aaa";
			return false;
		}else{//echo "bbb";
			return true;
		}
	}else{
		if(!in_array($domain_ip,$dnsRecord) && in_array('127.0.0.1',$dnsRecord)){//echo "ccc";
			return false;
		}else{//echo "ddd";
			return true;
		}
	}
	*/
	
}
include_once('config.php');
include_once('ips.php');
include_once('func_utils.php');
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
define('P_TPL', realpath(ROOT.DS.'../..'.DS.'templates/template_jingjian'));
define('PRE_SCRSHOT_URL', _urlprefix().URL_SCRSHOT);
define('PRE_DOWNLOAD_URL', _urlprefix().URL_DOWNLOAD);

$db = mysqli_connect(DB_HOST, DB_USER, DB_PASS);
mysqli_set_charset($db, DB_CHAR);
mysqli_select_db($db, DB_NAME);


$ezsite_uid = $_GET['ezsite_uid'];
$tplid = $_GET['tplid'];
$tag = stripslashes($_GET['tag']);
$sour = unserialize($tag);
$_domain = $sour['domain'];
$sql = "select is_install from ezsite_new_templates where id=$tplid";
$query = mysqli_query($db,$sql);
$_arr = mysqli_fetch_assoc($query);
$tplinfo = array();

if($_arr['is_install']=='0'){
//array_push($sour,$client_ip);
	if(checkDns2($sour,$ips)){
		$checkdomain=1;
	}
	if($checkdomain==1){
		$ret_domain_check = file_get_contents("http://licence.sitestar.cn/sitestar_check_domain.php?domain=".$_domain);
		if($ret_domain_check != '10000'){
			die("error1");
		}else{
			if(checkDns($sour)){
				die("error2");
			}
		}
	}
}
$ezsite_uid = mysqli_real_escape_string($db, $ezsite_uid);
$tplid = mysqli_real_escape_string($db, $tplid);

$sql = "SELECT `ezsite_new_templates`.*, `purchased_templates`.`purchase_time`"
        ." FROM `ezsite_new_templates` LEFT JOIN `purchased_templates` ON"
        ." `ezsite_new_templates`.`id`=`purchased_templates`.`ezsite_template_id`"
        ." WHERE `ezsite_new_templates`.`id`='$tplid' AND"
        ." (`purchased_templates`.`ezsite_uid`='$ezsite_uid' OR "
        ."`ezsite_new_templates`.`isfree`='1')";
$rs = mysqli_query($db, $sql);
if ($rs && mysqli_num_rows($rs) == 1) {
	$tplinfo = mysqli_fetch_assoc($rs);
    $tplinfo['screenshot_url'] = PRE_SCRSHOT_URL.'/'.$tplinfo['screenshot'];
    $tplinfo['package_url'] = PRE_DOWNLOAD_URL.$tplinfo['id'];
}
mysqli_free_result($rs);

echo serialize($tplinfo);
?>